//
//  main.m
//  OC_06集合遍历和数组排序
//
//  Created by dllo on 15/12/8.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
    
        
# pragma mark - 知识点1 for遍历集合
#if 0
        NSArray *arr = @[@"zhang",@"wang",@"li"];
        for (int i = 0; i < arr.count; i++) {
            NSLog(@"%@", arr[i]);
        }
        
        NSDictionary *dic = @{@"name":@"tom",
                              @"age":@"20",
                              @"sex":@"male"};
        NSArray *keys = dic.allKeys;
        
        for (int i = 0; i < keys.count; i++) {
            NSString *key = keys[i];
            NSString *str = [dic objectForKey:key];
            NSLog(@"%@",str);
        }
        
        NSSet *set = [NSSet setWithObjects:@"zhang",@"wang",@"li",nil];
        NSArray *temp = set.allObjects;
        for (int i = 0; i<temp.count; i++) {
            NSLog(@"%@",temp[i]);
        }
#endif
        
        
        
# pragma mark - 知识点2 NSEnumerator 枚举器
      
#if 0
        NSArray *arr = @[@"wang",@"zhang",@"li"];
        
        /* 数组  字典  集合  都有一个枚举器方法 , 返回的是枚举器对象*/
        
        NSEnumerator *enumerator = [arr objectEnumerator];
        id object;
        while (object = [enumerator nextObject]) {
            NSLog(@"%@",object);
        }
        
        /*逆向*/
        
        NSEnumerator *reverse = [arr reverseObjectEnumerator];
        id result2;
        while (result2 = [reverse nextObject]) {
            NSLog(@"%@",result2);
        }
        
        /*字典*/
        NSDictionary *dic = @{@"name":@"zhang",
                              @"age":@"20",
                              @"sex":@"male"};
        
        NSEnumerator *enumDic = [dic objectEnumerator];
        id objectInDic;
        while (objectInDic = [enumDic nextObject]) {
            NSLog(@"%@",objectInDic);
        }
        
        
        NSSet *set = [NSSet setWithObjects:@"zhang",@"wang",@"li", nil];
        NSEnumerator *enumSet = [set objectEnumerator];
        id objectInSet;
        while (objectInSet = [enumSet nextObject]) {
            NSLog(@"%@",objectInSet);
        }
        

        
# pragma mark - 知识点3 for .... in 遍历集合
        
        /** 
         * 重点1. 对于数组,for in 枚举的是对象 
         * 2.对于字典,枚举的是key
         * 3.对于集合,枚举的是对象
         */
        
//        for (<#type *object#> in <#collection#>) {
//            <#statements#>
//        }
        
        
# pragma mark - 知识点4 数组排序
        
        NSArray *arr= @[@"wang",@"zhang",@"li"];
        
        //1.创建对象
        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:@"self" ascending:NO];
        //构造器
//        NSSortDescriptor *des = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
        //2.调用数组的排序方发
        NSArray *resultArr = [arr sortedArrayUsingDescriptors:@[descriptor]];
//        NSLog(@"%@",resultArr);
        //3.遍历数组
        for (NSString *str in resultArr ) {
            NSLog(@"%@",str);
        }

        Person *per1 = [[Person alloc]initWithName:@"zhangsan" age:20 sex:@"male"];
        Person *per2 = [[Person alloc]initWithName:@"lisi" age:21 sex:@"male"];
        Person *per3 = [[Person alloc]initWithName:@"xiaoming" age:18 sex:@"male"];
        Person *per4 = [[Person alloc]initWithName:@"Zang" age:20 sex:@"female"];
        Person *per5 = [[Person alloc]initWithName:@"Tom" age:71 sex:@"male"];
        
        NSArray *arr2 = @[per1,per2,per3,per4,per5];
        //需求   按照年龄升序排序  如果年龄相同  再按照姓名升序排序
        NSSortDescriptor *des = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
        NSSortDescriptor *des1 = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
        NSArray *arr = [arr2 sortedArrayUsingDescriptors:@[des,des1]];
        
        for (Person *per in arr) {
            NSLog(@"%@ %ld %@",per.name,per.age,per.sex);
        }

        
# pragma mark ** sortedArrayUsingSelector
        
        //1.数组中都是字符串对象
        
        NSArray *arr = @[@"zhangsan",@"lisi",@"wang"];
        
        /**
         * @param SEL 方法选择器,@selector(方法名).
         *               方法的返回值必须是NSComparisonResult.
         *  注意: 此方法是数组中元素的方法
         */
        NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
        
        //Person,  按照name 升序排序
        Person *per1 = [[Person alloc]initWithName:@"zhangsan" age:20 sex:@"male"];
        Person *per2 = [[Person alloc]initWithName:@"lisi" age:21 sex:@"male"];
        Person *per3 = [[Person alloc]initWithName:@"xiaoming" age:18 sex:@"male"];
        Person *per4 = [[Person alloc]initWithName:@"Zang" age:20 sex:@"female"];
        Person *per5 = [[Person alloc]initWithName:@"Tom" age:71 sex:@"male"];
        
        NSArray *arr2 = @[per1,per2,per3,per4,per5];
        
        NSArray *newArr2 = [arr2 sortedArrayUsingSelector:@selector(compareWithNameUP:)];
        
        for (Person *per in newArr2) {
            NSLog(@"%@",per.name);
        }
//
        for (NSString *string in newArr) {
            NSLog(@"%@",string);
        }
        NSArray *newArr3 = [arr2 sortedArrayUsingSelector:@selector(compareWithNameDown:)];
        for (Person *per in newArr3) {
            NSLog(@"%@",per.name);
        }

        //根据年龄升序排序
        Person *per1 = [[Person alloc]initWithName:@"zhangsan" age:20 sex:@"male"];
        Person *per2 = [[Person alloc]initWithName:@"lisi" age:21 sex:@"male"];
        Person *per3 = [[Person alloc]initWithName:@"xiaoming" age:18 sex:@"male"];
        Person *per4 = [[Person alloc]initWithName:@"Zang" age:20 sex:@"female"];
        Person *per5 = [[Person alloc]initWithName:@"Tom" age:71 sex:@"male"];
        
        NSArray *arr2 = @[per1,per2,per3,per4,per5];

        NSArray *arr = [arr2 sortedArrayUsingSelector:@selector(compareWithAgeUP:)];
        for (Person *per in arr) {
            NSLog(@"%@%ld",per.name,per.age);
        }
  
        
# pragma mark ** sortedArrayUsingComparator
        
        NSArray *arr = @[@"changgui",@"liuneng",@"zhaosi"];
        NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            
            return  [obj1 compare:obj2];
        }];
        for (NSString *str in newArr) {
            NSLog(@"%@",str);
        }
        
        Person *per1 = [[Person alloc]initWithName:@"zhangsan" age:20 sex:@"male"];
        Person *per2 = [[Person alloc]initWithName:@"lisi" age:21 sex:@"male"];
        Person *per3 = [[Person alloc]initWithName:@"xiaoming" age:18 sex:@"male"];
        Person *per4 = [[Person alloc]initWithName:@"Zang" age:20 sex:@"female"];
        Person *per5 = [[Person alloc]initWithName:@"Tom" age:71 sex:@"male"];
        
        NSArray *arr2 = @[per1,per2,per3,per4,per5];
        
        //按照Person的name 升序排序
        NSArray *newArr2 = [arr2 sortedArrayUsingComparator:^NSComparisonResult(Person*  _Nonnull obj1, Person*  _Nonnull obj2) {
            return [obj1.name compare:obj2.name];
            
        }];
        for (Person *per in newArr2) {
            NSLog(@"%@",per.name);
        }
        
        //按照Person的age 降序排序
        NSArray *ageDown = [arr2 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            Person *per1 = obj1;
            Person *per2 = obj2;
            if (per1.age > per2.age) {
                return NSOrderedAscending;
            }else if (per1.age < per2.age){
                return NSOrderedDescending;
            }else{
                return NSOrderedSame;
            }
            
        }];
        
        for (Person *per in ageDown) {
            NSLog(@"%@ %ld",per.name,per.age);
        }
        
#endif
        
#pragma mark - 知识点4 可变数组排序
        
        NSMutableArray *mArr = @[@"wang",@"zhang",@"li" ].mutableCopy;
        //升序排序
        [mArr sortUsingSelector:@selector(compare:)];
        
        NSLog(@"%@",mArr);
        
#pragma mark - 时间就是金钱
        
        // ADI: NSDate
        
        
        
       
        //对100万的样本进行排序, 花费时间?
//        
//        NSMutableArray *arr = [NSMutableArray array];
//        for (int i = 0; i < 1000000; i++) {
//            NSString *str = [NSString stringWithFormat:@"%ud",arc4random()];
//            [arr addObject:str];
//        }
//        NSDate *startdate = [NSDate date];
//        NSLog(@"%@",startdate);
        //数组排序
//        for (int i = 0; i < arr.count - 1; i++) {
//            for (int j = 0; j < arr.count - 1 - i; j++) {
//                if ([arr[j] intValue] > [arr[j + 1]intValue]) {
//                    
//                    [arr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
//                    
//                }
//            }
//        }
        
//        [arr sortedArrayUsingSelector:@selector(compare:)];
//        
//        
//        
//         NSDate *enddate2 = [NSDate date];
//        NSLog(@"%@",enddate2);
//        //计算两个时间对象的时间差
//        NSLog(@"%f",[enddate2 timeIntervalSinceDate:startdate]);
//        
//        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
    return 0;
}
